<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showDialogVisible('add')"
            >添加分类</el-button
          >
        </el-col>
      </el-row>
      <tree-table
        :data="cateList"
        :columns="columns"
        show-index
        index-text="#"
        :expand-type="false"
        :selection-type="false"
        border
        :show-row-hover="false"
      >
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            style="color: lightgreen"
            v-if="scope.row.cat_deleted === false"
          ></i>
          <i class="el-icon-error" style="color: red" v-else></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <template slot="controls" slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="showDialogVisible('edit', scope.row.cat_id)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="deleteCate(scope.row.cat_id)"
            >删除</el-button
          >
        </template>
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      :title="addEditTitle"
      :visible.sync="addEditDialogVisible"
      width="50%"
      @close="addEditClose"
    >
      <el-form
        :model="addEditForm"
        :rules="addEditFormRules"
        ref="addEditFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addEditForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:" v-if="addEditDialogType !== 'edit'">
          <el-cascader
            v-model="CateKeys"
            :options="dialogCateList"
            :props="cascaderProps"
            @change="handleChange"
            clearable
            change-on-select
          ></el-cascader>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addEditDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addEditClick">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      cateList: [],
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '分类状态',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'controls'
        }
      ],
      addEditDialogVisible: false,
      addEditDialogType: '',
      addEditForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      addEditFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      dialogCateList: [],
      CateKeys: [],
      cateId: '',
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      }
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    async getCateList () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取商品分类数据列表失败！')
      this.cateList = res.data.result
      this.total = res.data.total
      console.log(this.cateList)
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getCateList()
    },
    async showDialogVisible (type, id) {
      this.cateId = id
      this.addEditForm.cat_pid = 0
      this.addEditForm.cat_name = ''
      this.addEditForm.cat_level = 0
      this.addEditDialogType = type
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) return this.$message.error('获取商品分类数据列表失败！')
      this.dialogCateList = res.data
      if (type === 'edit') {
        const { data: re } = await this.$http.get('categories/' + id)
        if (re.meta.status !== 200) return this.$message.error('查询分类失败！')
        this.addEditForm = re.data
      }
      this.addEditDialogVisible = true
    },
    addEditClick () {
      this.$refs.addEditFormRef.validate(async valid => {
        if (!valid) return
        if (this.addEditDialogType === 'add') {
          const { data: res } = await this.$http.post('categories', {
            cat_pid: this.addEditForm.cat_pid,
            cat_name: this.addEditForm.cat_name,
            cat_level: this.addEditForm.cat_level
          })
          if (res.meta.status !== 201) return this.$message.error('添加分类失败！')
          this.$message.success('添加分类成功！')
        } else if (this.addEditDialogType === 'edit') {
          const { data: res } = await this.$http.put('categories/' + this.cateId, { cat_name: this.addEditForm.cat_name })
          if (res.meta.status !== 200) return this.$message.error('修改分类失败！')
          this.$message.success('修改分类成功！')
        }
        this.getCateList()
        this.addEditDialogVisible = false
      })
    },
    handleChange () {
      console.log(this.CateKeys)
      this.addEditForm.cat_pid = this.CateKeys ? this.CateKeys[this.CateKeys.length - 1] : 0
      this.addEditForm.cat_level = this.CateKeys.length
    },
    addEditClose () {
      this.$refs.addEditFormRef.resetFields()
      this.CateKeys = []
    },
    async deleteCate (id) {
      const confirmResult = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message('已取消删除！')
      const { data: res } = await this.$http.delete('categories/' + id)
      if (res.meta.status !== 200) return this.$message.error('删除分类失败！')
      this.$message.success('删除分类成功！')
      if (this.cateList.length === 1 && this.queryInfo.pagenum > 1) {
        this.queryInfo.pagenum--
        this.getCateList()
      }
    }
  },
  computed: {
    addEditTitle () {
      return this.addEditDialogType === 'add' ? '添加分类' : '修改分类'
    }
  }
}
</script>
<style scoped>
.el-cascader {
  width: 100%;
}
</style>
